package com.han.controllers.export;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;

import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jmesa.limit.ExportType;
import org.jmesa.model.TableModel;
import org.jmesa.view.component.Column;
import org.jmesa.view.component.Row;
import org.jmesa.view.component.Table;
import org.jmesa.view.html.component.HtmlColumn;
import org.jmesa.view.html.component.HtmlRow;
import org.jmesa.view.html.component.HtmlTable;

import com.han.bom.Answer;
import com.han.bom.ExamResponse;
import com.han.bom.MultipleChoice;
import com.han.dao.concrete.product.AnswerDAO;
import com.han.dao.concrete.product.ExamResponseDAO;
import com.han.dao.concrete.product.QuestionDAO;

/**
 * Servlet implementation class JMesaExportStudentExamServlet
 */
@WebServlet("/Professor/JMesaExportStudentExamServlet")
public class JMesaExportStudentExamServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public JMesaExportStudentExamServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String examResponseID=null;
		
		if(request.getParameter("id")!=null){
			examResponseID = request.getParameter("id");
		}else if(request.getSession().getAttribute("erID")!=null){
			examResponseID = (String) request.getSession().getAttribute("erID");
		}
		else{
			//error
		}
		//System.out.println("SWERVLET: "+examResponseID);
		QuestionDAO questiondao = new QuestionDAO();
		AnswerDAO answerdao = new AnswerDAO();
		ExamResponseDAO examresponsedao = new ExamResponseDAO();
		
		ExamResponse tempER = null;
		
		try {
			tempER = examresponsedao.retrieveResponseById(examResponseID);

			for(Answer answer: tempER.getAnswers()){
				answer.getQuestion().setQuestion(questiondao.retrieveQuestionById(answer.getQuestion().getQuestionID()).getQuestion());
				
				ArrayList<String> choices = answerdao.retrieveAnswersByQuestion(answer.getQuestion().getQuestionID());
				answer.getQuestion().setAnswer(choices.get(0));
				
				if(answer.getQuestion() instanceof MultipleChoice){
					Collections.sort(choices);
				}
				else{
					Collections.sort(choices);
					Collections.reverse(choices);
				}
			
				answer.getQuestion().setChoices(choices);

			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		TableModel tableModel = new TableModel("table", request, response);
		
		tableModel.setExportTypes(ExportType.CSV,ExportType.JEXCEL,ExportType.PDFP);
		tableModel.setItems(tempER.getAnswers());
		
		if(tableModel.isExporting()){
			
			Table Table = new Table().caption("Examinee-Exam Record");
			
			Row Row = new Row();
			Table.setRow(Row);
			
			Column id = new Column("questionID").title("Question ID");
			Row.addColumn(id);
	
			Column lastName = new Column("questionText").title("Question");
			Row.addColumn(lastName);
	
			Column term = new Column("chosenAnswer").title("Chosen Answer");
			//tableModel.addFilterMatcher(new MatcherKey(Double.class, "studentGrade"), new NumberFilterMatcher("#.##"));
			Row.addColumn(term);
	
			Column career = new Column("correctAnswer").title("Correct Answer");
			Row.addColumn(career); 
	
			tableModel.setTable(Table);
			
			
			String export = tableModel.render();
			
			request.setAttribute("table",export);
		}
		else{
			HtmlTable htmlTable = new HtmlTable();
	
			HtmlRow htmlRow = new HtmlRow();
			htmlTable.setRow(htmlRow);
						
			HtmlColumn studentIDCol = new HtmlColumn("questionID").title("Question ID");
	    	htmlRow.addColumn(studentIDCol); 
	
			HtmlColumn examIDCol = new HtmlColumn("questionText").title("Question");
	    	htmlRow.addColumn(examIDCol);
	    	
	    	HtmlColumn dateCol = new HtmlColumn("chosenAnswer").title("Chosen Answer");
	    	htmlRow.addColumn(dateCol);
	
	    	HtmlColumn gradeCol = new HtmlColumn("correctAnswer").title("Correct Answer");
	    	htmlRow.addColumn(gradeCol);
	
			tableModel.setTable(htmlTable);
	
			String html = tableModel.render();
			
			request.setAttribute("table",html);
			request.setAttribute("examresponse", tempER);
			RequestDispatcher rd = request.getRequestDispatcher("Report_StudentsAnswer.jsp");
			rd.forward(request, response);	
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
